package com.igeek.health.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.health.constant.MessageConstant;
import com.igeek.health.entity.Checkitem;
import com.igeek.health.exception.HealthException;
import com.igeek.health.result.Result;
import com.igeek.health.service.ICheckitemService;
import com.igeek.health.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author fengqq
 * @Description TODO
 * @Date 2024/8/26  11:34
 */
@RestController
@RequestMapping("/admin/checkitem")
//只有admin角色才能访问
@PreAuthorize("hasRole('ADMIN')")
@Api(tags = "检查项的控制层")
public class CheckitemController {
    @Autowired
    private ICheckitemService checkitemService;

    @ApiOperation("根据条件分页查询检查项")
    @GetMapping("/findPage")
    public Result findPage(@ApiParam("分页查询条件") PageQueryVO vo){
        Page<Checkitem> page =  checkitemService.findPage(vo);
        if (ObjectUtil.isNotEmpty(page)){
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    @ApiOperation("新建检查项")
    @PostMapping("/add")
    public Result add(@ApiParam("检查项信息") @RequestBody Checkitem checkitem){
        boolean flag = checkitemService.save(checkitem);
        if(flag){
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        }
        return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
    }

    @ApiOperation("根据检查项id查询检查项详情")
    @GetMapping("/{id}")
    public Result findOne(@ApiParam("检查项id") @PathVariable("id") Integer id){
        Checkitem checkitem = checkitemService.getById(id);
        if(ObjectUtil.isNotEmpty(checkitem)){
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitem);
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);

    }

    @ApiOperation("根据检查项id更新信息")
    @PutMapping("/update")
    public Result update(@ApiParam("检查项信息") @RequestBody Checkitem checkitem){
        boolean flag = checkitemService.updateById(checkitem);
        if(flag){
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }
        return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    @ApiOperation("根据检查项id删除信息")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        boolean flag = checkitemService.delete(id);
        if(flag){
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }
        return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
    }

    @ApiOperation("查询所有检查项信息")
    @GetMapping("/findAll")
    public Result findAll(){
        List<Checkitem> checkitemList = checkitemService.findAll();
        if(CollectionUtil.isNotEmpty(checkitemList)){
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemList);
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }

}
